package com.com.corejavalearnings.generics.designpattern.visitor;

/**
 * Created by User on 13/07/2015.
 */
public abstract class Tree<E>
{
    public static <E> Tree<E> leaf(final E elt)
    {
        return new Tree<E>()
        {
            @Override
            public <R> R visit(Visitor<E,R> visitor)
            {
                return visitor.visitLeaf(elt);
            }
        };
    }

    public static <E> Tree<E> branch(final Tree<E> left, final Tree<E> right)
    {
        return new Tree<E>()
        {
            @Override
            public <R> R visit(Visitor<E,R> visitor)
            {
                return visitor.visitBranch(left.visit(visitor), right.visit(visitor));

            }
        };
    }

    public static interface Visitor<E, R>
    {
        public <R> R visitLeaf(E elt);
        public <R> R visitBranch(R left, R right);
    }

    public abstract <R> R visit(Visitor<E, R> visitor);
}

class TreeClient {
    /*public static <T> String toString (Tree<T> t) {
        return t.visit(new Tree.Visitor<T, String>()
        {
            @Override
            public <R> R visitLeaf(T elt) {
                return null;
            }

            @Override
            public <R> R visitBranch(R left, R right) {
                return null;
            }
        }
    })*/
}

